/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <gtest/gtest.h>

#include "hks_client_ipc_serialization.h"
#include "hks_log.h"

using namespace testing::ext;
namespace Unittest::HksClientIpcSerializationTest {
class HksClientIpcSerializationTest : public testing::Test {
public:
    static void SetUpTestCase(void);

    static void TearDownTestCase(void);

    void SetUp();

    void TearDown();
};

void HksClientIpcSerializationTest::SetUpTestCase(void)
{
}

void HksClientIpcSerializationTest::TearDownTestCase(void)
{
}

void HksClientIpcSerializationTest::SetUp()
{
}

void HksClientIpcSerializationTest::TearDown()
{
}

/**
 * @tc.name: HksClientIpcSerializationTest.HksClientIpcSerializationTest001
 * @tc.desc: tdd CopyUint32ToBuffer, expect HKS_ERROR_BUFFER_TOO_SMALL
 * @tc.type: FUNC
 */
HWTEST_F(HksClientIpcSerializationTest, HksClientIpcSerializationTest001, TestSize.Level0)
{
    HKS_LOG_I("enter HksClientIpcSerializationTest001");

    uint32_t index = 15;
    const uint32_t destBlobSize = 10;
    uint8_t destBlobData[destBlobSize] = { 0 };
    struct HksBlob destBlob = { .size = destBlobSize, .data = destBlobData };

    int32_t ret = CopyUint32ToBuffer(0, &destBlob, &index);
    EXPECT_EQ(ret, HKS_ERROR_BUFFER_TOO_SMALL);
}

/**
 * @tc.name: HksClientIpcSerializationTest.HksClientIpcSerializationTest002
 * @tc.desc: tdd HksOnceParamPack CopyBlobToBuffer, expect HKS_ERROR_BUFFER_TOO_SMALL
 * @tc.type: FUNC
 */
HWTEST_F(HksClientIpcSerializationTest, HksClientIpcSerializationTest002, TestSize.Level0)
{
    HKS_LOG_I("enter HksClientIpcSerializationTest002");

    uint32_t index = 15;
    const uint32_t destBlobSize = 10;
    uint8_t destBlobData[destBlobSize] = { 0 };
    struct HksBlob destBlob = { .size = destBlobSize, .data = destBlobData };

    int32_t ret = HksOnceParamPack(&destBlob, nullptr, nullptr, &index);
    EXPECT_EQ(ret, HKS_ERROR_BUFFER_TOO_SMALL);
}

/**
 * @tc.name: HksClientIpcSerializationTest.HksClientIpcSerializationTest003
 * @tc.desc: tdd HksAgreeKeyPack CopyParamSetToBuffer, expect HKS_ERROR_BUFFER_TOO_SMALL
 * @tc.type: FUNC
 */
HWTEST_F(HksClientIpcSerializationTest, HksClientIpcSerializationTest003, TestSize.Level0)
{
    HKS_LOG_I("enter HksClientIpcSerializationTest003");

    const uint32_t destBlobSize = 10;
    uint8_t destBlobData[destBlobSize] = { 0 };
    struct HksBlob destBlob = { .size = destBlobSize, .data = destBlobData };
    const struct HksParamSet paramSet = { .paramSetSize = 12 };

    int32_t ret = HksAgreeKeyPack(&destBlob, &paramSet, nullptr, nullptr, nullptr);
    EXPECT_EQ(ret, HKS_ERROR_BUFFER_TOO_SMALL);
}

/**
 * @tc.name: HksClientIpcSerializationTest.HksClientIpcSerializationTest004
 * @tc.desc: tdd HksGetKeyInfoListUnpackFromService GetUint32FromBuffer, expect HKS_ERROR_BUFFER_TOO_SMALL
 * @tc.type: FUNC
 */
HWTEST_F(HksClientIpcSerializationTest, HksClientIpcSerializationTest004, TestSize.Level0)
{
    HKS_LOG_I("enter HksClientIpcSerializationTest004");

    const uint32_t destBlobSize = 2;
    uint8_t destBlobData[destBlobSize] = { 0 };
    struct HksBlob destBlob = { .size = destBlobSize, .data = destBlobData };

    int32_t ret = HksGetKeyInfoListUnpackFromService(&destBlob, nullptr, nullptr);
    EXPECT_EQ(ret, HKS_ERROR_BUFFER_TOO_SMALL);
}

/**
 * @tc.name: HksClientIpcSerializationTest.HksClientIpcSerializationTest005
 * @tc.desc: tdd HksCertificateChainUnpackFromService GetBlobFromBuffer, expect HKS_ERROR_IPC_MSG_FAIL
 * @tc.type: FUNC
 */
HWTEST_F(HksClientIpcSerializationTest, HksClientIpcSerializationTest005, TestSize.Level0)
{
    HKS_LOG_I("enter HksClientIpcSerializationTest005");

    const uint32_t srcBlobSize = 7;
    uint8_t srcBlobData[srcBlobSize] = { 4 };
    struct HksBlob srcBlob = { .size = srcBlobSize, .data = srcBlobData };
    struct HksCertChain certChain = { .certsCount = 4 };

    int32_t ret = HksCertificateChainUnpackFromService(&srcBlob, false, &certChain);
    EXPECT_EQ(ret, HKS_ERROR_IPC_MSG_FAIL);
}

/**
 * @tc.name: HksClientIpcSerializationTest.HksClientIpcSerializationTest006
 * @tc.desc: tdd EncodeCertChain CheckAndCalculateSize
 * @tc.type: FUNC
 */
HWTEST_F(HksClientIpcSerializationTest, HksClientIpcSerializationTest006, TestSize.Level0)
{
    HKS_LOG_I("enter HksClientIpcSerializationTest006");

    struct HksBlob inBlob = { .size = UINT32_MAX };
    int32_t ret = EncodeCertChain(&inBlob, nullptr);
    EXPECT_EQ(ret, HKS_ERROR_INVALID_ARGUMENT);

    inBlob.size = UINT32_MAX - 2;
    ret = EncodeCertChain(&inBlob, nullptr);
    EXPECT_EQ(ret, HKS_ERROR_INVALID_ARGUMENT);

    inBlob.size = 3221225431;
    ret = EncodeCertChain(&inBlob, nullptr);
    EXPECT_EQ(ret, HKS_ERROR_INVALID_ARGUMENT);
}

/**
 * @tc.name: HksClientIpcSerializationTest.HksClientIpcSerializationTest007
 * @tc.desc: tdd EncodeCertChain CheckAndCalculateSize Base64Encode
 * @tc.type: FUNC
 */
uint8_t inBlobData[] = {
    0x30, 0x82, 0x03, 0x13, 0x30, 0x82, 0x02, 0xb9, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x0e, 0x63,
    0xcb, 0x7d, 0xcd, 0xb3, 0x86, 0x85, 0x27, 0xc6, 0xbc, 0xe0, 0x4d, 0x33, 0x99, 0x30, 0x0a, 0x06,
    0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x30, 0x5d, 0x31, 0x39, 0x30, 0x37, 0x06,
    0x03, 0x55, 0x04, 0x03, 0x0c, 0x30, 0x48, 0x75, 0x61, 0x77, 0x65, 0x69, 0x20, 0x43, 0x42, 0x47,
    0x20, 0x45, 0x43, 0x43, 0x20, 0x44, 0x65, 0x76, 0x69, 0x63, 0x65, 0x20, 0x41, 0x6e, 0x6f, 0x6e,
    0x79, 0x6d, 0x6f, 0x75, 0x73, 0x20, 0x41, 0x74, 0x74, 0x65, 0x73, 0x74, 0x61, 0x74, 0x69, 0x6f,
    0x6e, 0x20, 0x43, 0x41, 0x20, 0x31, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
    0x0a, 0x48, 0x75, 0x61, 0x77, 0x65, 0x69, 0x20, 0x43, 0x42, 0x47, 0x31, 0x0b, 0x30, 0x09, 0x06,
    0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x43, 0x4e, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x34, 0x30, 0x38,
    0x30, 0x38, 0x30, 0x32, 0x30, 0x35, 0x35, 0x37, 0x5a, 0x17, 0x0d, 0x32, 0x34, 0x30, 0x38, 0x31,
    0x35, 0x30, 0x32, 0x30, 0x35, 0x35, 0x37, 0x5a, 0x30, 0x2c, 0x31, 0x2a, 0x30, 0x28, 0x06, 0x03,
    0x55, 0x04, 0x03, 0x0c, 0x21, 0x44, 0x65, 0x76, 0x69, 0x63, 0x65, 0x20, 0x43, 0x65, 0x72, 0x74,
    0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x65, 0x20, 0x4d, 0x61, 0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65,
    0x6e, 0x74, 0x20, 0x4b, 0x65, 0x79, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce,
    0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00,
    0x04, 0xea, 0xba, 0xbf, 0x64, 0x55, 0x32, 0x59, 0x2b, 0xd6, 0xe2, 0x95, 0xea, 0x06, 0x4f, 0x35,
    0xe9, 0x58, 0x48, 0x68, 0x68, 0x9e, 0x55, 0x1e, 0xf4, 0xad, 0x3e, 0x62, 0x39, 0xad, 0xd4, 0xba,
    0x7b, 0x99, 0xbc, 0x0c, 0x9e, 0x8f, 0xc3, 0x77, 0x8f, 0x57, 0x01, 0xd8, 0x8a, 0xa6, 0x5d, 0x5b,
    0xe3, 0xfd, 0x0e, 0x23, 0xff, 0x1a, 0xe5, 0xbe, 0x6e, 0xd4, 0x73, 0x1d, 0xc4, 0x00, 0xe3, 0x9a,
    0x08, 0xa3, 0x82, 0x01, 0x8c, 0x30, 0x82, 0x01, 0x88, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e,
    0x04, 0x16, 0x04, 0x14, 0x55, 0xdc, 0x0d, 0xb0, 0x00, 0xa6, 0x36, 0x92, 0xa0, 0x9c, 0x93, 0xb5,
    0x8f, 0xd8, 0x68, 0x17, 0x44, 0x71, 0x1b, 0x16, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01,
    0x01, 0xff, 0x04, 0x02, 0x30, 0x00, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30,
    0x16, 0x80, 0x14, 0xe3, 0x2c, 0xcb, 0xff, 0x76, 0x87, 0x3b, 0x12, 0xfb, 0x43, 0x22, 0x3f, 0x3f,
    0xfb, 0x02, 0x06, 0x81, 0xdf, 0x27, 0xa7, 0x30, 0x82, 0x01, 0x36, 0x06, 0x0c, 0x2b, 0x06, 0x01,
    0x04, 0x01, 0x8f, 0x5b, 0x02, 0x82, 0x78, 0x01, 0x03, 0x04, 0x82, 0x01, 0x24, 0x30, 0x82, 0x01,
    0x20, 0x02, 0x01, 0x00, 0x30, 0x81, 0xcf, 0x02, 0x01, 0x02, 0x06, 0x0d, 0x2b, 0x06, 0x01, 0x04,
    0x01, 0x8f, 0x5b, 0x02, 0x82, 0x78, 0x02, 0x01, 0x03, 0x30, 0x81, 0xba, 0x06, 0x0e, 0x2b, 0x06,
    0x01, 0x04, 0x01, 0x8f, 0x5b, 0x02, 0x82, 0x78, 0x02, 0x01, 0x03, 0x01, 0x04, 0x81, 0xa7, 0x7b,
    0x22, 0x61, 0x70, 0x70, 0x49, 0x64, 0x22, 0x3a, 0x22, 0x63, 0x6f, 0x6d, 0x2e, 0x65, 0x78, 0x61,
    0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x6d, 0x79, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69,
    0x6f, 0x6e, 0x5f, 0x42, 0x4e, 0x73, 0x72, 0x6a, 0x4d, 0x73, 0x67, 0x4a, 0x54, 0x30, 0x4f, 0x61,
    0x59, 0x50, 0x49, 0x36, 0x70, 0x75, 0x57, 0x70, 0x7a, 0x74, 0x2b, 0x67, 0x43, 0x36, 0x71, 0x44,
    0x55, 0x73, 0x51, 0x78, 0x33, 0x78, 0x77, 0x49, 0x51, 0x67, 0x68, 0x4a, 0x4a, 0x6a, 0x7a, 0x6d,
    0x66, 0x4d, 0x49, 0x58, 0x59, 0x38, 0x6f, 0x32, 0x2b, 0x49, 0x57, 0x56, 0x79, 0x48, 0x37, 0x43,
    0x2f, 0x61, 0x63, 0x53, 0x2f, 0x44, 0x4a, 0x6f, 0x43, 0x57, 0x78, 0x41, 0x74, 0x44, 0x51, 0x4c,
    0x2b, 0x51, 0x35, 0x78, 0x36, 0x2b, 0x34, 0x78, 0x2f, 0x41, 0x3d, 0x22, 0x2c, 0x22, 0x62, 0x75,
    0x6e, 0x64, 0x6c, 0x65, 0x4e, 0x61, 0x6d, 0x65, 0x22, 0x3a, 0x22, 0x63, 0x6f, 0x6d, 0x2e, 0x65,
    0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x6d, 0x79, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61,
    0x74, 0x69, 0x6f, 0x6e, 0x22, 0x7d, 0x30, 0x22, 0x02, 0x01, 0x00, 0x06, 0x0d, 0x2b, 0x06, 0x01,
    0x04, 0x01, 0x8f, 0x5b, 0x02, 0x82, 0x78, 0x02, 0x01, 0x04, 0x04, 0x0e, 0x63, 0x68, 0x61, 0x6c,
    0x6c, 0x65, 0x6e, 0x67, 0x65, 0x5f, 0x64, 0x61, 0x74, 0x61, 0x30, 0x25, 0x02, 0x01, 0x03, 0x06,
    0x0e, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x8f, 0x5b, 0x02, 0x82, 0x78, 0x02, 0x02, 0x02, 0x06, 0x04,
    0x10, 0x28, 0xc4, 0xfb, 0x49, 0x44, 0xaf, 0xec, 0x11, 0xb9, 0x09, 0x02, 0x42, 0xac, 0x12, 0x00,
    0x02, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x03, 0x48, 0x00,
    0x30, 0x45, 0x02, 0x21, 0x00, 0x92, 0x41, 0xa8, 0xf7, 0xb9, 0x57, 0x79, 0x78, 0x0d, 0xd8, 0xf1,
    0x76, 0xaf, 0x10, 0x4e, 0xef, 0xce, 0xc5, 0xff, 0xbe, 0x8b, 0x04, 0x86, 0xb4, 0xd4, 0xa5, 0x11,
    0x13, 0x16, 0xc5, 0xf8, 0xa1, 0x02, 0x20, 0x3b, 0xb2, 0x22, 0x50, 0xf5, 0x10, 0x76, 0x05, 0x98,
    0xf7, 0x5a, 0xeb, 0xe3, 0x92, 0xfe, 0x29, 0x31, 0x0f, 0xc8, 0x3a, 0x04, 0xf1, 0x97, 0xbf, 0x39,
    0x3a, 0x5d, 0xf5, 0xe3, 0xb7, 0x35, 0xa1
};
HWTEST_F(HksClientIpcSerializationTest, HksClientIpcSerializationTest007, TestSize.Level0)
{
    HKS_LOG_I("enter HksClientIpcSerializationTest007");

    uint32_t inBlobSize = sizeof(inBlobData) / sizeof(inBlobData[0]);
    struct HksBlob inBlob = { .size = inBlobSize, .data = inBlobData };
    uint32_t outBlobSize = 4096;
    uint8_t outBlobData[outBlobSize];
    for (uint32_t i = 0; i < inBlobSize; ++i) {
    outBlobData[i] = inBlobData[i];
    }
    struct HksBlob outBlob = { .size = outBlobSize, .data = outBlobData };

    int32_t ret = EncodeCertChain(&inBlob, &outBlob);
    EXPECT_EQ(ret, HKS_SUCCESS);
}

/**
 * @tc.name: HksClientIpcSerializationTest.HksClientIpcSerializationTest008
 * @tc.desc: tdd HksCertificateChainUnpackFromService
 * @tc.type: FUNC
 */
HWTEST_F(HksClientIpcSerializationTest, HksClientIpcSerializationTest008, TestSize.Level0)
{
    HKS_LOG_I("enter HksClientIpcSerializationTest008");

    const uint32_t srcBlobSize1 = 2;
    uint8_t srcBlobData1[srcBlobSize1] = { 5 };
    struct HksBlob srcBlob = { .size = srcBlobSize1, .data = srcBlobData1 };
    struct HksCertChain certChain = { .certsCount = 4 };

    int32_t ret = HksCertificateChainUnpackFromService(&srcBlob, true, &certChain);
    EXPECT_EQ(ret, HKS_ERROR_BUFFER_TOO_SMALL);

    const uint32_t srcBlobSize2 = 4;
    uint8_t srcBlobData2[srcBlobSize2] = { 5 };
    srcBlob.size = srcBlobSize2;
    srcBlob.data = srcBlobData2;
    ret = HksCertificateChainUnpackFromService(&srcBlob, true, &certChain);
    EXPECT_EQ(ret, HKS_ERROR_INSUFFICIENT_DATA);
}

/**
 * @tc.name: HksClientIpcSerializationTest.HksClientIpcSerializationTest009
 * @tc.desc: tdd HksListAliasesUnpackFromService
 * @tc.type: FUNC
 */
HWTEST_F(HksClientIpcSerializationTest, HksClientIpcSerializationTest009, TestSize.Level0)
{
    HKS_LOG_I("enter HksClientIpcSerializationTest009");

    struct HksBlob srcBlob = { .size = 0, .data = nullptr };
    int32_t ret = HksListAliasesUnpackFromService(&srcBlob, nullptr);
    EXPECT_EQ(ret, HKS_SUCCESS);
}
}
